package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class TicketAssign extends Composite {

	public TicketAssign(Integer tcktId) {
		final Integer ticketId = tcktId ;
		
		ArrayList labels = new ArrayList();

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		verticalPanel.add(verticalPanel_1);
		verticalPanel_1.setSpacing(5);
		verticalPanel_1.setStyleName("textbox");
		
		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_1);
		horizontalPanel_1.setSpacing(2);

		final Label lblAutor = new Label("Autor:");
		horizontalPanel_1.add(lblAutor);
		lblAutor.setStyleName("labelTicket");

		final Label lblAutorText = new Label("");
		horizontalPanel_1.add(lblAutorText);
		lblAutorText.setStyleName("labelTicketText");

		final Label labelComma = new Label(",");
		labelComma.setWidth("10px");
		horizontalPanel_1.add(labelComma);
		labelComma.setStyleName("labelTicketText");
		
		final Label lblDataText = new Label("");
		horizontalPanel_1.add(lblDataText);
		lblDataText.setStyleName("labelTicketText") ;
		lblDataText.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_5);
		horizontalPanel_5.setSpacing(2);

		final Label lblStatus = new Label("Estado:");
		horizontalPanel_5.add(lblStatus);
		lblStatus.setStyleName("labelTicket");

		final Label lblStatusText = new Label("");
		horizontalPanel_5.add(lblStatusText);
		lblStatusText.setStyleName("labelTicketText");	

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);

		final Label lblTitle = new Label("Título:");
		horizontalPanel_2.add(lblTitle);
		lblTitle.setStyleName("labelTicket");

		final Label lblTitleText = new Label("");
		horizontalPanel_2.add(lblTitleText);
		lblTitleText.setStyleName("labelTicketText");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_3);
		horizontalPanel_3.setSpacing(2);

		final Label lblQuestion = new Label("Pergunta:");
		horizontalPanel_3.add(lblQuestion);
		lblQuestion.setStyleName("labelTicket");

		final Label lblQuestionText = new Label("");
		horizontalPanel_3.add(lblQuestionText);
		lblQuestionText.setStyleName("labelTicketText");
		
		labels.add(lblQuestionText);
		labels.add(lblTitleText);
		labels.add(lblStatusText);
		labels.add(lblAutorText);
		labels.add(lblDataText);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setSpacing(5);

		final ListBox listAssist = new ListBox();
		horizontalPanel.add(listAssist);
		listAssist.setWidth("200");
		
		ServiceTickets.Util.getInstance().loadDataForAssignment(ticketId, new LoadTicketDataAsyncCallback(labels,listAssist)) ;

		final Button btnAtribuir = new Button();
		horizontalPanel.add(btnAtribuir);
		btnAtribuir.setStyleName("button");
		btnAtribuir.setText("Atribuir");
		
		btnAtribuir.addClickListener(new ClickListener() {
    		public void onClick(final Widget sender){
    			ServiceTickets.Util.getInstance().assignTicket(new Integer(listAssist.getValue(listAssist.getSelectedIndex())),
    															new Integer((String)Default.getSession("IDPerson")),
    															ticketId,
    															new assignTicketCallback());
    		}
    	}) ;
		
	}
	
	static class assignTicketCallback implements AsyncCallback {
		
		assignTicketCallback() {}
		
		public void onFailure(Throwable caught) {
			System.out.println("AssingTicket :: FALHOU a registar a atribuicao!");
		}

		public void onSuccess(Object result) {
			Default.loadComposite(new TicketsViewPending());
		}
	}
	
	static class LoadTicketDataAsyncCallback implements AsyncCallback {
		ArrayList labels ;
		ListBox listAssist ;

		LoadTicketDataAsyncCallback(ArrayList labels, ListBox listAssist)
		{
			this.labels = labels ;
			this.listAssist = listAssist ;
		}
		
		public void onFailure(Throwable caught) {
			System.out.println("AssingTicket :: FALHOU a ir buscar dados!");
		}

		public void onSuccess(Object result) {
			ArrayList assignmentdata = (ArrayList) result ;
			  
			printTicketInfo((ArrayList)assignmentdata.get(0));
			addAssistantsToList((ArrayList)assignmentdata.get(1)) ;
		}
		
		/**
		 * Prints the ticket info on the labels included in objs field
		 * 
		 * @param ticket ArrayList with ticket info
		 */
		private void printTicketInfo(ArrayList ticket)
		{
	    	  if(ticket.size()>0)
	    	  {
	    		  for (int i = 0 ; i < labels.size(); i++)
	    		  {
	    			  Label lbl = (Label)labels.get(i);
	    			  lbl.setText(ticket.get(i).toString());
	    		  }
	    	  }
	    	  else
	    	  {
	    		  System.out.println("Nao tem Info sobre o Ticket");
	    	  }
		}
		
		private void addAssistantsToList(ArrayList assistants)
		{
			System.out.println("addAssistantsToList :: assistants size " + assistants.size());
			for (int i = 0 ; i < assistants.size() ; i++)
			{
				ArrayList assist = (ArrayList) assistants.get(i);

				listAssist.addItem((String)assist.get(0), (String)assist.get(1));
			}
		}
	}

}
